﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RoguelikeLib;

namespace Roguelike
{
    class Renderer : IRenderer
    {
        # region Fields
        private readonly Form _form;
        private Label[][] _grid;
        Random _rng = new Random();
        private string[] _charactersArray = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "." };
        private Color[] _colors = new Color[] { Color.Red, Color.Blue, Color.Purple, Color.Yellow, Color.Green };

        private int _tileWidth = 15;
        private int _tileHeight = 15;
        # endregion

        # region Properties

        public int Height
        {
            get { return 40; }
        }

        public int HeightInPixels
        {
            get { return Height * _tileHeight + _tileHeight; }
        }

        public int Width
        {
            get { return 80; }
        }

        public int WidthInPixels
        {
            get { return Width * _tileWidth + _tileWidth; }
        }

        # endregion

        # region Constructor

        public Renderer(Form form)
        {
            _form = form;
            CreateGrid();
        }

        # endregion

        # region Public Methods

        public void Clear()
        {
            throw new NotImplementedException();
        }

        public void CreateGrid()
        {
            var labelSize = new Size(_tileWidth, _tileHeight);
            _grid = new Label[Height][];
            for (int i = 0; i < Height; i++)
            {
                var row = new Label[Width];
                _grid[i] = row;
                for (int j = 0; j < Width; j++)
                {
                    var label = new Label()
                    {
                        Location = new Point(j * labelSize.Width, i * labelSize.Height),
                        Size = labelSize,
                        BackColor = Color.Black,
                        ForeColor = Color.White,
                        Text = ""
                    };
                    row[j] = label;
                    _form.Controls.Add(label);
                }
            }
        }

        public void Draw(int x, int y, RObjects thingToDraw)
        {
            var character = _charactersArray[_rng.Next(_charactersArray.Length)];
            var label = _grid[y][x];
            label.Text = character;
            label.ForeColor = _colors[_rng.Next(_colors.Length)]; 
            label.Refresh();
        }

        # endregion
        
        /// <summary>
        /// Randomly update the map (Christmas lights!)
        /// </summary>
        public void UpdateGrid()
        {
            int x = _rng.Next(Width);
            int y = _rng.Next(Height);
            Draw(x, y, RObjects.GrassFloor);
        }

    }
}
